package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Trucks;
import com.edu.service.TrucksService;
import com.edu.service.Impl.TrucksServiceImpl;

/**
 * Servlet implementation class TrucksServletAll
 */
@WebServlet("/TrucksServletAll")
public class TrucksServletAll extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("1111111111");
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByTrucks(request,response);
			System.out.println("查询成功!");
		//修改
		}else if("2".equals(menu)) {
			trucksUpdate(request, response);
			System.out.println("修改成功!");
		//添加
		}else if("3".equals(menu)) {
			trucksAdd(request, response);
			System.out.println("添加成功!");
		//删除
		}else if("4".equals(menu)) {
			trucksDelete(request, response);
			System.out.println("删除成功!");
		}else {
			System.out.println("sorry!");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	//查询
	protected void queryByTrucks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int tId = -1;
		if(!("".equals(request.getParameter("tId")))){
			tId = Integer.parseInt(request.getParameter("tId"));
		}
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tMain = request.getParameter("tMain");
		String tAge = request.getParameter("tAge");
		String tState = request.getParameter("tState");
		Trucks trucks = new Trucks(tId, tType, tNum, tMain, tAge, tState);
		TrucksService trucksServlet = new TrucksServiceImpl();
		List<Trucks> list = trucksServlet.selectByTrucks(trucks);
		request.setAttribute("list",list);
		//response.sendRedirect("Trucks.jsp");
		request.getRequestDispatcher("Trucks.jsp").forward(request, response);
	}
	
	//修改
	protected void trucksUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int tId = Integer.parseInt(request.getParameter("tId"));
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tMain = request.getParameter("tMain");
		String tAge = request.getParameter("tAge");
		String tState = request.getParameter("tState");
		Trucks trucks = new Trucks(tId, tType, tNum, tMain, tAge, tState);
		TrucksService trucksServlet = new TrucksServiceImpl();
		trucksServlet.updateTrucks(trucks);
		//response.sendRedirect("Trucks.jsp");
		request.getRequestDispatcher("Trucks.jsp").forward(request, response);
	}
	
	//添加 
	protected void trucksAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String tType = request.getParameter("tType");
		String tNum = request.getParameter("tNum");
		String tMain = request.getParameter("tMain");
		String tAge = request.getParameter("tAge");
		String tState = request.getParameter("tState");
		Trucks trucks = new Trucks(tType, tNum, tMain, tAge, tState);
		TrucksService trucksServlet = new TrucksServiceImpl();
		trucksServlet.addTrucks(trucks);
		response.sendRedirect("Trucks.jsp");
	}
	
	//删除
	protected void trucksDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selectTid");

		TrucksService trucksServlet = new TrucksServiceImpl();
		trucksServlet.deleteTrucksAll(ids);
		response.sendRedirect("Trucks.jsp");
	}

}
